import BaseConfig, { IItemConfig } from "../../../Script/Base/BaseConfig";
import BaseSprite from "../../../Script/Base/BaseSprite";
import CustomButton from "../../../Script/Common/CustomButton";
import { EResType } from "../../../Script/Common/Enum";
import EventID from "../../../Script/Common/EventID";
import { IBagItemData, IMessage } from "../../../Script/Interface/IMessage";
import { LoadUtil } from "../../../Script/Util/LoadUtil";
import { SceneGame } from "../../SceneGame";
import MarketUI from "../market/MarketUI";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemBag extends BaseSprite implements IMessage {

    @property(cc.Sprite)
    private icon: cc.Sprite = null;

    @property(cc.Label)
    private labNum: cc.Label = null;

    @property(cc.Label)
    private labName: cc.Label = null;

    private config: IItemConfig = null;
    private itemData: IBagItemData = null;
    private openType: '背包' | '地摊' = '背包';

    recvMsg(cmd: number, data: any): void {
        switch (cmd) {
            case EventID.TEST_CMD:
                {

                }
                break;
        }
    }

    protected onLoad() {
        this.addMessage(EventID.TEST_CMD, this);
    }

    public setData(data: IBagItemData, openType: '背包' | '地摊') {
        this.openType = openType;
        this.itemData = data;
        this.config = BaseConfig.instance.getItemInfoById(data.id);
        this.labName.string = `${this.config.name}`;
        this.labNum.string = `${data.num}`;
        LoadUtil.instance.setSpriteFrame(this.icon, EResType.Item, data.id, 65, 65);

        // 背包按钮时设置为不可点
        this.node.getComponent(CustomButton).enabled = this.openType === '地摊';
    }

    public getType() {
        return this.config.type;
    }

    public getData() {
        return this.itemData;
    }

    private onTouchClicked(event, eventData) {
        switch (this.openType) {
            case '背包':
                {
                    // BagUI.instance.showItemInfo(this.config, this.itemData);
                }
                break;
            case '地摊':
                {
                    this.node.active = false;
                    MarketUI.instance.refreshBag(this.config, this.itemData);
                    MarketUI.instance.hideBagViewAnim();
                }
                break;
        }
    }
}